import { getInfoApi, loginApi, getDetailUserInfoApi } from "@/api/user";
import { setToken, getToken, removeToken } from "@/utils/auth";
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {},
  },
  mutations: {
    setToken(state, data) {
      state.token = data;
      // 存入cookie实现持久化
      setToken(data);
    },
    removeToken(state) {
      state.token = null;
      removeToken();
    },
    setUserInfo(state, data) {
      state.userInfo = data;
    },
    removeUserInfo(state) {
      state.userInfo = {};
    },
  },
  actions: {
    // 登录
    async loginAction(context, data) {
      let res = await loginApi(data);
      console.log(res);
      context.commit("setToken", res);
    },
    // 获取用户资料
    async getUserInfoAction(context) {
      let res = await getInfoApi();
      let res2 = await getDetailUserInfoApi(res.userId);
      context.commit("setUserInfo", { ...res, ...res2 });
    },
    // 退出
    quitAction(context) {
      context.commit("removeToken");
      context.commit("removeUserInfo");
    },
  },
};
